<?php
declare (strict_types = 1);
namespace app\promotion\model;

use app\common\model\BaseModel;
use app\user\logic\user\Account;
use think\model\concern\SoftDelete;

/**
 * 活动模型
 */
class PromotionBargain extends BaseModel
{
    use softDelete;
    /**
     * 表名
     * @var string
     */
    protected $name = 'promotion_bargain';

    /**
     * 定义字段类型
     * @var array
     */
    protected $type = [
        'id'                    => 'integer',
        'promotion_id'          => 'integer',
        'product_id'            => 'integer',
        'product_type_id'       => 'integer',
        'bargained_nums'        => 'integer',
        'bargained_total_price' => 'float',
        'remain_price'          => 'float',
        'remain_nums'           => 'integer',
        'done'                  => 'integer',
        'create_time'           => 'integer',
        'delete_time'           => 'integer',
    ];

    // protected $visible = ['current_user_id', 'current_bargain_price'];
    
    /**
     * 发起人查询范围
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-08
     * @param  [type] $query [description]
     * @return [type]        [description]
     */
    public function scopeSponsor($query, $sponsor_user_id = 0)
    {
        $sponsor_user_id = $sponsor_user_id ? : request()->mid;
        $query->whereSponsorUserId($sponsor_user_id);
    }

    /**
     * 活动条件查询范围
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-08
     * @param  [type] $query [description]
     * @return [type]        [description]
     */
    public function scopePromotionId($query, $promotion_id = 0)
    {
        $query->wherePromotionId($promotion_id);
    }
    
    /**
     * 砍价用户数据
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-12
     * @return [type] [description]
     */
    public function bargainData()
    {
        return $this->hasMany(PromotionBargainData::class, 'bargain_id')->with(['user_avatar_name'])->hidden(['id', 'bargain_id']);
    }
    
    /**
     * 砍价用户数据
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-12
     * @return [type] [description]
     */
    public function currentBargain()
    {
        return $this->hasOne(PromotionBargainData::class, 'bargain_id')->bind(['current_bargain_price', 'bargain_id' => 'id']);
    }

    /**
     * 砍价条件数据
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-12
     * @return [type] [description]
     */
    public function bargainCondition()
    {
        return $this->belongsTo(Promotion::class, 'promotion_id', 'id');//->bind(['num']);
    }

}
